package application;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;

public class MediaDemo extends Application{

	private static final String MEDIA_URL = "https://liveexample.pearsoncmg.com/common/sample.mp4";
	//private static final String localURL = "D:/Study/Repository/Gitee/swufe_Java/JavaFX_UI_Controls_and_Multimedia/example.mp4";
	private static final String localURL = "D:/Study/Repository/Gitee/swufe_Java/JavaFX_UI_Controls_and_Multimedia/example.mp4";
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		
		Media media = new Media("file:///" + localURL);
		MediaPlayer mediaPlayer = new MediaPlayer(media);
		MediaView mediaView = new MediaView(mediaPlayer);
		mediaView.setFitWidth(1920 * 0.5);
		mediaView.setFitHeight(1080 * 0.5);
		
		
		Button playButton = new Button(">");
		playButton.setOnAction(e -> {
			if (playButton.getText().equals(">")) {
				mediaPlayer.play();
				playButton.setText("||");
			}
			else {
				mediaPlayer.pause();
				playButton.setText(">");
			}
		});
		
		Button rewindButton = new Button("<<");
		rewindButton.setOnAction(e -> mediaPlayer.seek(Duration.ZERO));
		
		Slider slVolume = new Slider();
		slVolume.setPrefWidth(150);
		slVolume.setMaxWidth(Region.USE_COMPUTED_SIZE);
		slVolume.setMinWidth(20);
		slVolume.setValue(50);
		mediaPlayer.volumeProperty().bind(slVolume.valueProperty().divide(100));
		
		HBox hBox = new HBox(10);
		hBox.setAlignment(Pos.CENTER);
		hBox.getChildren().addAll(playButton, rewindButton, new Label("Volume"), slVolume);
		
		BorderPane pane = new BorderPane();
		pane.setCenter(mediaView);
		pane.setBottom(hBox);
		
		Scene scene = new Scene(pane, 1920 * 0.5, 1080 * 0.5 + 50);
		primaryStage.setTitle("MediaDemo");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Application.launch(args);
		
		
	}

}
